package com.example.demo.web.controller;

import com.example.demo.domain.base.BaseResult;
import com.example.demo.domain.base.PageResult;
import com.example.demo.domain.param.SysDictCreateParam;
import com.example.demo.domain.param.SysDictQueryParam;
import com.example.demo.domain.param.SysDictUpdateParam;
import com.example.demo.domain.vo.SysDictInfoVO;
import com.example.demo.domain.vo.SysDictTreeVO;
import com.example.demo.domain.vo.SysDictVO;
import com.example.demo.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * SysDictController
 *
 * @author haochen78
 * @date 2025/10/3 22:52
 **/
@RestController
@RequestMapping("/dict")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    @PostMapping
    public BaseResult<Void> create(@Validated @RequestBody SysDictCreateParam param) {
        sysDictService.create(param);
        return BaseResult.success();
    }

    @PutMapping
    public BaseResult<Void> update(@Validated @RequestBody SysDictUpdateParam param) {
        sysDictService.update(param);
        return BaseResult.success();
    }

    @DeleteMapping("/{id}")
    public BaseResult<Void> delete(@PathVariable Long id) {
        sysDictService.delete(id);
        return BaseResult.success();
    }

    @GetMapping("/{id}")
    public BaseResult<SysDictVO> getById(@PathVariable Long id) {
        return BaseResult.success(sysDictService.getById(id));
    }

    @PostMapping("/list")
    public BaseResult<PageResult<SysDictVO>> list(@RequestBody SysDictQueryParam param) {
        return BaseResult.success(sysDictService.list(param));
    }

    @GetMapping("/type/{typeCode}")
    public BaseResult<List<SysDictInfoVO>> getByType(@PathVariable String typeCode) {
        return BaseResult.success(sysDictService.getDictByType(typeCode));
    }

    @GetMapping("/tree/{typeCode}")
    public BaseResult<List<SysDictTreeVO>> getTreeByType(@PathVariable String typeCode) {
        return BaseResult.success(sysDictService.buildDictTree(typeCode));
    }
}
